<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-calendar"></i> 消息管理 </el-breadcrumb-item>
                <el-breadcrumb-item>信息修改</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div>
                <el-form ref="form" :model="form" label-width="100px" :rules="rules">

                    <el-form-item label="发送人编号" prop="tid">
                        <el-input v-model="form.tid" disabled class="input_width"></el-input>
                    </el-form-item>

                    <el-form-item label="发送人姓名" prop="name" >
                        <el-input v-model="form.name" disabled class="input_width"></el-input>
                    </el-form-item>

                    <el-form-item label="消息类型" prop="type">
                        <el-select v-model="form.type" placeholder="请选择类型">
                            <el-option key="1" label="普通消息" value="00" ></el-option>
                            <el-option key="2" label="通知消息" value="01" ></el-option>
                        </el-select>
                    </el-form-item>


                    <el-form-item label="接收人" prop="receivers">
                      <el-tag v-for="(item,index) in msgEmp" :key="index">{{item.tid+'-'+item.name}}</el-tag>
                    </el-form-item>

                    <el-form-item label="状态" prop="title">
                        <el-switch
                            style="display: block"
                            v-model="form.status"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            active-value="01"
                            inactive-value="00"
                            active-text="正常"
                            inactive-text="已删除">
                        </el-switch>
                    </el-form-item>

                     <el-form-item label="信息标题" prop="title" >
                        <el-input v-model="form.title" class="input_width"></el-input>
                    </el-form-item>

                    <el-form-item label="内容" prop="content">
                        <mavon-editor v-model="form.content" ref="md" @imgAdd="$imgAdd" @change="textChange" style="min-height: 600px"/>
                    </el-form-item>

                    <el-form-item>
                        <el-button @click="goback" icon="el-icon-arrow-left">返回</el-button>
                        <el-button type="primary" @click="onSubmit('form')" icon="el-icon-edit">更改</el-button>
                        <el-button type="warning" @click="reset('form')" icon="el-icon-refresh-right">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import { mavonEditor } from 'mavon-editor'
    import 'mavon-editor/dist/css/index.css'
export default {
    name: 'baseform',
    inject: ['reload'],
    data() {
        return {
            id:this.$route.query.id,
            
            depts:[],
            positions:[],
            employees:[],
            message:{},
            msgEmp:[],
            
            form: {
                id:'',
                tid:'',
                name: '',
                type:'',
                title:'',
                receivers:[],
                html:'',
                content: '',
                createdTime:'',
                status:'',
            },
            receivers:[],
             rules: {
                tid: [
                    { required: true, message: '请填入发件人编号', trigger: 'blur' },
                ],
                name: [
                    { required: true, message: '请填入发件姓名', trigger: 'blur' },
                ],
                type: [
                    { required: true, message: '请选择信息类型', trigger: 'blur' },
                ],
               
                title: [
                    { required: true, message: '请填入消息标题', trigger: 'blur' },
                ],
                content: [
                    { required: true, message: '请填入消息内容', trigger: 'blur' },
                ],


            }
        };
    },
    components: {
            mavonEditor
    },
    created: function () {
        this.getMsgEmp();
        this.init();
    
        
    },
    methods: {

        

        // 把编译好的值存入form中
        textChange(value, render){
            this.form.html=render;
        },

        // 将图片上传到服务器，返回地址替换到md中
        $imgAdd(pos, $file){
            // var formdata = new FormData();
            // formdata.append('file', $file);
            // // 这里没有服务器供大家尝试，可将下面上传接口替换为你自己的服务器接口
            // this.$axios({
            //     url: '/images',
            //     method: 'post',
            //     data: formdata,
            //     headers: { 'Content-Type': 'multipart/form-data' },
            // }).then((url) => {
            //     this.$refs.md.$img2Url(pos, url);
            // })
        },
       
      
        goback() {
            this.$router.go(-1);
        },
      
        //初始化表单
        async init(){
           var d = await this.$http.get("/msg/"+this.id)
           if (d.code==200) {
               this.message = d.data
               this.initForm(this.message);
           }
        },

        async getMsgEmp(){
            var d = await this.$http.get("/msgEmpByMid/"+this.id)
            if (d.code==200) {
                this.msgEmp=d.data
            }
        },

        initForm(msg){
            this.form.id=msg.id;
            this.form.tid=msg.tid;
            this.form.name=msg.name;
            this.form.type=msg.type;
            this.form.title=msg.title;
            this.form.html=msg.html;
            this.form.content=msg.content;
            this.form.receivers=msg.receivers;
            this.form.status=msg.status;
            
        },


         onSubmit(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {

                    var d = await this.$http.put('/msg', this.form);
                    if (d.code == 200) {
                        this.$message.success(d.message);
                        this.reload();
                    } else {
                        this.$message.error(d.message);
                    }
                } else {
                    this.$message.warning('请把内容填写完整!');
                }
            });
        },

        reset(formName) {
            this.$confirm('此操作将重置所有信息, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                  this.initForm(this.message)
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消重置'
                    });
                });
        }


    }
};
</script>
<style scoped>
.input_width{
    width: 300px;
}
</style>